package com.example.test2;

import android.app.Application;
import android.content.Context;


import com.example.test2.db.control.ClothesDbController;
import com.example.test2.db.control.UserDbController;
import com.example.test2.db.entity.Clothes;
import com.example.test2.db.entity.ClothesInfo;
import com.example.test2.db.entity.User;
import com.example.test2.http.Constants;
import com.example.test2.utils.Tools;

import java.util.ArrayList;
import java.util.List;

// TODO: 2024/6/02  app
public class App extends Application {

    private static Context context;
    private ClothesDbController clothesDbController;

    @Override
    public void onCreate() {
        super.onCreate();
        context = this;
        new Thread(() -> initData()).start();//初始化数据
    }

    private void initData() {
        clothesDbController = ClothesDbController.getInstance(this);
        List<Clothes> list = clothesDbController.searchAll();
        if (list.size() == 0) {
            initClothes();
            initUser();
        }
    }

    private void initClothes() {
        for (int i = 0; i < 30; i++) {
            ClothesInfo clothesInfo = ClothesInfo.getClothesInfoList().get(i);
            Clothes clothes = new Clothes(clothesInfo.getName(),
                    clothesInfo.getType(),
                    clothesInfo.getPrice(),
                    clothesInfo.getType().equals("1") ? "新鲜保质" :
                            clothesInfo.getType().equals("2") ? "专属果蔬" : "清凉一夏",
                    "拼少少商家" + (i + 1), Tools.generatePhoneNumber(),
                    Tools.getNum(100, 200) + "",
                    "吉林省长春市朝阳区硅谷街道" + (i + 1),
                    "坏一赔四·价保服务·不支持无理由退换",
                    "包邮:承诺12小时内发货");
            clothesDbController.insertOrReplace(clothes);
        }
    }

    private void initUser() {
        User user = new User();
        List<String> list2 = new ArrayList<>();
        list2.add("今天能发货吗?");
        user.setPhone(Constants.PHONE);
        user.setShopName(Constants.PHONE);
        user.setBoy(true);
        user.setPwd("123456");
        user.setMsg("中午的饭真好吃");
        user.setDate("2024-06-03");
        user.setMsgList(list2);
        UserDbController.getInstance(this).insertOrReplace(user);

        User user2 = new User();
        user2.setPhone(Tools.generatePhoneNumber());
        user2.setMsg("祝自己儿童节快乐！");
        user2.setBoy(false);
        user2.setDate("2024-06-01");
        user2.setAttention(true);
        UserDbController.getInstance(this).insertOrReplace(user2);

        User user3 = new User();
        user3.setPhone(Tools.generatePhoneNumber());
        user3.setBoy(true);
        user3.setMsg("诸事顺利！");
        user3.setDate("2024-06-02");
        user3.setAttention(false);
        UserDbController.getInstance(this).insertOrReplace(user3);

        User user4 = new User();
        user4.setPhone(Tools.generatePhoneNumber());
        user4.setBoy(true);
        user4.setMsg("累累累累累");
        user4.setDate("024-06-02");
        user4.setAttention(false);
        UserDbController.getInstance(this).insertOrReplace(user4);
    }

    public static Context getContext() {
        return context;
    }


}


